package psn.qiuyiping.autocad.dxffile;

import java.util.Arrays;

public class SectionHeader extends Section {
    String                    key;

    public SectionHeader() {
        key = null;
    }

    public void readDxfBody(DxfProcessContext context, DxfData item) {
        if (item.code == 9) {
            key = ((String) item.getVal()).substring(1);
            context.push(this::readValue);
        } else {
            throw new IllegalArgumentException(item.toString());
        }
    }

    public void readValue(DxfProcessContext context, @SuppressWarnings("rawtypes") DxfData item) {
        context.getFile().setSetting(key, item.getVal());
        key = null;
        context.pop();
    }

    public static String settingToString(DxfProcessContext context) {
        StringBuilder sb = new StringBuilder("SectionHeader [");
        DxfFile dxf = context.getFile();
        for (String key : dxf.keySet()) {
            sb.append(key);
            sb.append("=");
            Object value =dxf.getSetting(key);
            if (value instanceof double[]) {
                sb.append(Arrays.toString((double[]) value));
            } else {
                sb.append(value);
            }
            sb.append(", ");
        }
        sb.setLength(sb.length() - 2);
        sb.append("]");
        return sb.toString();
    }
    
    @Override
    public void afterBlockEnd(DxfProcessContext context) {
        // TODO Auto-generated method stub
        super.afterBlockEnd(context);
        System.out.println(settingToString(context));
    }
}
